package org.phoenixframework.api.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.phoenixframework.api.sys.service.ISysDeptService;
import org.phoenixframework.api.sys.util.DeptUtil;
import org.phoenixframework.common.entity.SysDept;
import org.phoenixframework.common.entity.annotation.OperationLog;
import org.phoenixframework.common.tools.ResultModel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统部门表 前端控制器
 * </p>
 *
 * @author doublelife
 * @since 2021-05-08
 */
@Slf4j
@Api(tags = {"系统部门"})
@AllArgsConstructor
@RestController
@RequestMapping("/sysDept")
public class SysDeptController extends BaseController {

    private final ISysDeptService sysDeptService;

    @OperationLog
    @ApiOperation("获取全部数据")
    @GetMapping("/findAll")
    public ResultModel<?> findAll() {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(SysDept::getSort);
        List<SysDept> list = sysDeptService.list(wrapper);
        return this.buildByList(list);
    }

    @OperationLog
    @ApiOperation("获取全部数据（树形）")
    @GetMapping("/findAllTree")
    public ResultModel<?> findAllTree() {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(SysDept::getSort);
        List<SysDept> list = sysDeptService.list(wrapper);
        return this.buildByList(DeptUtil.build(list));
    }

    @OperationLog
    @ApiOperation("保存信息")
    @PostMapping("/save")
    public ResultModel<?> save(@ApiParam(name = "SysDept", value = "对象信息", required = true) @Validated @RequestBody SysDept sysDept) {
        boolean result = sysDeptService.save(sysDept);
        return this.buildByBool(result);
    }

    @ApiOperation("根据id获取一条信息")
    @GetMapping("/find/{id}")
    public ResultModel<?> findById(@ApiParam(name = "id", value = "对象信息id", required = true) @PathVariable Long id) {
        SysDept sysDept = sysDeptService.getById(id);
        return this.buildByObject(sysDept);
    }

    @OperationLog
    @ApiOperation("根据id修改信息")
    @PutMapping("/update")
    public ResultModel<?> updateById(@ApiParam(name = "SysDept", value = "对象信息", required = true) @Validated @RequestBody SysDept sysDept) {
        if (StringUtils.isBlank(sysDept.getId().toString())) {
            return ResultModel.error();
        }
        boolean result = sysDeptService.updateById(sysDept);
        return this.buildByBool(result);
    }

    @OperationLog
    @ApiOperation("根据id删除一条信息(递归删除子级)")
    @DeleteMapping("/remove/{id}")
    public ResultModel<?> removeById(@ApiParam(name = "id", value = "对象信息id", required = true) @PathVariable Long id) {
        boolean b = sysDeptService.removeChildrenById(id);
        return this.buildByBool(b);
    }

    @OperationLog
    @ApiOperation("检查部门编码是否存在")
    @GetMapping("/validCodeExits")
    public ResultModel<?> validCodeExits(@RequestParam(name = "id", required = false) Long id,
                                         @RequestParam("code") String code) {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(null != id || StringUtils.isNotBlank(String.valueOf(id)), SysDept::getId, id);
        wrapper.eq(SysDept::getCode, code);
        List<SysDept> list = sysDeptService.list(wrapper);
        return list.size() > 0 ? ResultModel.ok(Boolean.FALSE) : ResultModel.ok();
    }

}
